<?php

namespace App\Http\Controllers;

use App\Helpers\Code;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Log;

class Controller extends BaseController
{

    use AuthorizesRequests,
        DispatchesJobs,
        ValidatesRequests;

    protected $model;
    protected $rules = [];
    protected $messages = [];
    protected $result = [
        'code' => Code::SYSTEM_OK,
        'data' => [],
        'msg' => []
    ];
    protected $pregPhone = "/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199)\d{8}$/";

    /**
     * 搜索查询
     * @param Request $request
     * @param $model 模型
     * @return array
     */
    protected function handleSearch(Request $request, $model)
    {
        $search = [];

        foreach ($model->params as $value) {
            if ($request->has($value)) {
                $input = $request->input($value);
                if ($input !== null) {
                    $search[$value] = $input;
                }
            }
        }
        return $search;
    }

    /**
     * 排序 格式   字段_排序  例  id_asc
     * @param Request $request
     * @return array
     */
    protected function handleSort(Request $request, $sorters = [])
    {
        $sorter = ['created_at', 'asc'];
        $orderBy = $request->input('sorter');
        if ($orderBy) {
            $underline = strrpos($orderBy, '_');
            if ($underline) {
                $sorter[0] = substr($orderBy, 0, $underline);

                if (!in_array($sorter[0], $sorters)) {
                    $sorter[0] = 'created_at';
                }

                $pre = substr($orderBy, $underline + 1, 1);
                $sorter[1] = $pre === 'a' ? 'asc' : 'desc';
            }
        }
        return $sorter;
    }

    /**
     * 判断是否本人操作
     * @param $inputId   输入的id
     * @param $userId    登陆的id
     * @return array
     */
    protected function isMyself($inputId, $userId)
    {
        if ($inputId != $userId) {
            return false;
        }

        return true;
    }

    protected function checked($data, $restricts = [])
    {
        foreach ($restricts as $key => $message) {
            if ($data === null) {
                $this->result['code'] = Code::SYSTEM_CONFINE_ERROR;
                if (isset($restricts['null'])) {
                    $this->result['msg'] = trans($restricts['null']);
                } else {
                    $this->result['msg'] = trans('common.handle_error');
                }
                return;
            } elseif ($data === $key) {
                $this->result['code'] = Code::SYSTEM_CONFINE_ERROR;
                $this->result['msg'] = trans($message);
                return;
            }
        }

        return $this->result['data'] = $data;
    }

    protected function error($message, $code = Code::SYSTEM_CONFINE_ERROR)
    {
        $this->result['code'] = $code;

        switch ($code) {
            case Code::SYSTEM_VALIDATOR_ERROR:
                $this->result['msg'] = error($message->errors()->toArray());
                break;
            case Code::SYSTEM_CONFINE_ERROR:
            case Code::SYSTEM_STATUS_ERROR:
            default :
                $this->result['msg'] = trans($message);
        }

        return $this->result;
    }

    /**
     * 捕获抛出异常
     * @param $e
     * @return array
     */
    protected function throwError($e)
    {
        Log::channel('errordaily')->error($e->getFile(). ' 第' . $e->getLine() . '行，错误信息：' .$e->getMessage());
        $this->result['code'] = Code::SYSTEM_CONFINE_ERROR;
        $this->result['msg'] = trans('common.database_error');
        return $this->result;
    }

}
